<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/page.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if($_POST['consumer_id'] != ''){		
		$db = new Class_DB;
		//データ上に前面通過
		if($_POST['mode']=='input'){
			$date = date('Y/m/d G:i:s');
		
			$consumer_id = $_POST['consumer_id'];
			$company_name = $_POST['company_name'];
			$katakana = $_POST['katakana'];
			$branch_name = $_POST['branch_name'];
			$category = $_POST['category'];
			$sal_manager = $_POST['sal_manager'];
			$payment_term = $_POST['payment_term'];	
			$r_date = $_POST['r_date'];
			$u_date = $date;
			$list = array("consumer_id"=>$consumer_id,
						"company_name"=>$company_name,
						"katakana"=>$katakana,
						"branch_name"=>$branch_name,
						"category"=>$category,
						"sal_manager"=>$sal_manager,
						"payment_term"=>$payment_term,
						"r_date"=>$r_date,
						"u_date"=>$u_date);
			
			//SQLクエリー
			$sql = "SELECT * FROM p_consumer_head where producer ="."(SELECT sal_manager FROM p_consumer WHERE consumer_id='".$consumer_id."')";	
			
			$res = $db->query($sql);

			
			$cnt = 0;
				
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
				//1生産、②承認申請、3は承認、4承認が却下;5カスタマーOK]を、6顧客のNG
				if($row['status']==1){
					$opt = "<label><input type=\"hidden\" name=\"status\" value=\"1\">生産</input></label>";

				}elseif ($row['status']==2){
					$opt = "<label><input type=\"hidden\" name=\"status\" value=\"2\">承認申請</input></label>";

				}elseif ($row['status']==3){
					$opt = "<label><input type=\"hidden\" name=\"status\" value=\"3\">は承認</input></label>";

				}elseif ($row['status']==4){
					$opt = "<label><input type=\"hidden\" name=\"status\" value=\"4\">承認が却下</input></label>";

				}elseif ($row['status']==5){
					$opt = "<label><input type=\"hidden\" name=\"status\" value=\"5\">OK</input></label>";

				}else{
					$opt = "<label><input type=\"hidden\" name=\"status\" value=\"6\">NG</input></label>";

				}
				$row['opt'] = $opt;
					
				$pricelist[] = $row;
				$cnt++;
			}
			
		}elseif($_POST['mode']=='update'){
			//アップデート
			$date = date('Y/m/d G:i:s');
							
			//SQLクエリー
			$sql = "UPDATE p_consumer SET 
				company_name='".$_POST['company_name'].
				"',katakana='".$_POST['katakana'].
				"',branch_name='".$_POST['branch_name'].
				"',category='".$_POST['category'].
				"',sal_manager='".$_POST['sal_manager'].
				"',payment_term='".$_POST['payment_term']."',u_date='".$date."' WHERE consumer_id='".$_POST['consumer_id']."'";	
						
			$db->query( $sql );		
			header("Location:./consumer_index.php");
		}	
	}else{
		header("Location:./consumer_index.php");
	}
}

else{
	
	if($_GET['consumer_id'] != ''){
		
		$db = new Class_DB;
		
		if($_GET['mode']=="delete"){
			
			$sql = "DELETE FROM p_consumer WHERE consumer_id ='".$_GET['consumer_id']."'";
			$db->query( $sql );	
			//header("Location:./consumer_index.php");	
			$phase = 'completedelete';
			$smarty->assign('phase', $phase);
		}else{
			
			//SQLクエリー
			$sql = "SELECT * FROM p_consumer where consumer_id =".$_GET['consumer_id'];
					
			$res = $db->query($sql);
			
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
				$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
				$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
				$consumerlist[] = $row;
			}
			
			$consumerlist[0]['id'] = sprintf('%08d', $consumerlist[0]['consumer_id']);
			
			$smarty->assign('list', $consumerlist[0]);
			$consumer_id = $_GET['consumer_id'];
			
			//SQLクエリー 見積歴史
			$query = "&consumer_id=$consumer_id";
			$current = 1;
			if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
				$current = $_GET['page'];
				$st = ($current -1) * PAGE_SIZE;
			}else{
				$current = 1;
				$st = 0;
			}
			
			$sql = "SELECT * FROM p_consumer_head where status=5 and  consumer_id=$consumer_id ORDER BY price_no DESC LIMIT $st, ".PAGE_SIZE;
			$res = $db->query($sql);
			
			
			
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
				$opt = "OK";
				$row['opt'] = $opt;
				$row['m_date']=$row['m_date']==NULL?"":date('Y/m/d',strtotime($row['m_date']));
				$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
				$row['e_date']=$row['e_date']==NULL?"":date('Y/m/d',strtotime($row['e_date']));
				$pricelist[] = $row;
				
			}
			
			$smarty->assign('pricelist', $pricelist);
			
			$sql = "SELECT * FROM M_STAFF";
			$res = $db->query($sql);
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
				$staff[$row['staff_id']] = $row['staff_name'];
			
			}
			
			$cnt = 0;
			$sqlcount ="SELECT COUNT(*) as cnt FROM p_consumer_head where status=5 and  consumer_id=$consumer_id";
			$db->query( $sqlcount );
			$row = $db->fetch();
			$cnt = $row['cnt'];
			
			$smarty->assign('page', getPage($current, $cnt,$query));
			$smarty->assign('staff', $staff);
		}
	}
}

$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->display('web-admin/consumerInfo.html');

?>